import Rpoup from '@/components/popup/popup' //公共弹窗组件

export default {
  components: {
    Rpoup, //弹窗
  },
  data() {
    return {
      mobile: '',
      code: '',
      second: 60, //验证码60S
      TypeSelector: ['中国大陆+86', '中国香港+852', '中国澳门+853', '中国台湾+886'], //类型弹窗列表
      TypeShow: false,
      num: 86,
      btnText: '获取验证码',
      interval: null, //倒计时
    }
  },
  computed: {
    isDisable(){
      if(this.mobile.length === 11 && this.code){
        return false
      }
      return true
    },
    isSend(){
      if(this.btnText === '获取验证码'){
        return true
      }
      return false
    },
    showClearIcon(){
      if(this.mobile){
        return true
      }
      return false
    },
  },
  onLoad() {},
  methods: {
    //打开区号picker
    ChooseSelector() {
      this.TypeShow = true
    },

    //取消关闭所有弹窗
    cancel() {
      this.TypeShow = false
    },
    //选择类型
    chooseType(index) {
      this.num = this.TypeSelector[index].substr(5)
      this.cancel() //关闭弹窗
    },

    //倒计时
    getCode() {
      let times = 60
      this.interval = setInterval(() => {
			  if(times > 1) {
			    times--
			    this.btnText = times < 10 ? '0' + times + 's' : times + 's' //小于10秒补 0
			  } else {
			    clearInterval(this.interval)
			    this.btnText = '获取验证码'
			    this.interval = null
			  }
      }, 1000)
    },

    //发送验证码
    async sendCode() {
      if(!(/^1[3456789]\d{9}$/.test(this.mobile))){
			  uni.showToast({
				  title: '请输入正确的手机号',
				  icon: 'none',
			  })
        return
      }
      try {
        const data = await this.$api.user.pwdSendCode({
          mobile: this.mobile
        })
        this.getCode()
        uni.showToast({
          title: '发送成功',
          icon: 'none'
        })
      }catch (err){
        console.log(err)
      }
    },
    // 点击小叉叉
    clearIcon(){
      this.mobile = ''
    },

    blur() {
      if(!(/^1[3456789]\d{9}$/.test(this.mobile))){
        uni.showToast({
      	  title: '请输入正确的手机号',
      	  icon: 'none',
        })
      }
    },

    //提交
    async submit(){
      try{
        const res = await this.$api.user.updateMobile({
          NewMobile: this.mobile,
          IdentityCode: this.code
        })
        const _this = this
        if(this.interval){
          clearInterval(this.interval)
        }
        uni.showToast({
          title: '修改成功,请重新登录',
          icon: 'none',
          duration: 3000
        })
        setTimeout(function(){
          _this.logout()
        },3000)
      }catch(e){
        //TODO handle the exception
      }
    },

    //退出登录
    logout(){
		  this.$store.commit('resetInfo')
		  this.$store.commit('user/resetInfo')
		  let promise = this.tim.logout()
		  promise.then(res => {
		    this.$store.commit('news/reset')
		  }).catch(err => {
				 console.log('退出失败')
		  })
		  uni.removeStorage({
		    key: 'token',
		  })
		  uni.removeStorage({
		    key: 'Ztc_B_USER_INFO',
		    success: function (res) {
		      uni.redirectTo({
		        url: '/pages_login/login/login?number=' + this.mobile
		      })
		    }
		  })

    }
  }
}
